<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>组建通信-props的驼峰标识</title>
</head>
<body>
<div id="app">
  <cpn :child-my-message='message' :child-user-info='userInfo' @emit-edit-message="cpnEmitFunc"></cpn>

  <h3>父组件消息值：{{message}}</h3>
  <h3>父组件用户信息值：{{userInfo.name}} - {{userInfo.age}} - {{userInfo.height}}</h3>
</div>

<template id="template-cpn">
  <div>
    <h2>{{childMyMessage}}</h2>
    <h2>{{childUserInfo}}</h2>
    <button @click="incrementAge" :disabled="childUserInfo.age>100">年龄+1</button>
    <button @click="decrementAge" :disabled="childUserInfo.age<=1">年龄-1</button>
    <button @click="editMessage">修改信息</button>
  </div>
</template>
<script src="../js/vue.js"></script>
<script>

  const cpn = {
    template: '#template-cpn',
    props: {
      childMyMessage: {
        type: String,
        default: ''
      },
      childUserInfo: {
        type: Object,
        default() {
          return {name: "张三", age: 20, height: 185}
        }
      }
    },
    methods: {
      incrementAge() {
        this.childUserInfo.age++
      },
      decrementAge() {
        this.childUserInfo.age--
      },
      editMessage() {
        // this.childMyMessage = "aaaa"; // 不能这么做
        this.$emit('emit-edit-message', "aaaaa")
      }
    },
  }

  const app = new Vue({
    el: '#app',
    data: {
      message: '请问你是谁',
      userInfo: {
        name: 'why',
        age: 18,
        height: 1.80
      }
    },
    components: {
      cpn
    },
    methods: {
      cpnEmitFunc(arg) {
        this.message = arg;
      }
    }
  });
</script>
</body>
</html>
